/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rw.ac.kiosc.jdbc.school.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import rw.ac.kiosc.jdbc.school.dao.UserDAO;
import rw.ac.kiosc.jdbc.school.orm.User;

/**
 *
 * @author antoine
 */
public class UserHibernateDAOImpl extends DAO implements UserDAO {

    public static UserHibernateDAOImpl instance;

    public static UserHibernateDAOImpl getInstance() {
        if (instance == null) {
            instance = new UserHibernateDAOImpl();
        }
        return instance;
    }

    public User saveUser(User user) {
        try {
            begin();
            User usr = (User) getSession().save(user);
            commit();
            return usr;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public User updateUser(User user) {
        try {
            begin();
            getSession().update(user);
            commit();
            return user;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public User saveOrUpdateUser(User user) {
        try {
            begin();
            getSession().saveOrUpdate(user);
            commit();
            return user;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public boolean deleteUser(User user) {
        try {
            begin();
            getSession().delete(user);
            commit();
            return true;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return false;
        }
    }

    public User getUserById(int userId) {
        try {
            begin();
            Query q = getSession().createQuery("from User where userId = :ref");
            q.setInteger("ref", userId);
            User usr = (User) q.uniqueResult();
            commit();
            return usr;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public List<User> getAllUsers() {
        try {
            begin();
            List<User> users = (List<User>) getSession().createQuery("from User").list();
            commit();
            return users;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<User>();
        }
    }

    public User getUserByUsername(String username) {
        try {
            begin();
            Query q = getSession().createQuery("from User where username = :ref");
            q.setString("ref", username);
            User usr = (User) q.uniqueResult();
            commit();
            return usr;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public List<User> getUsersByName(String name) {
        try {
            begin();
            Query q = getSession().createQuery("from User where name like :ref");
            q.setString("ref", name);
            List<User> users = (List<User>) q.list();
            commit();
            return users;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<User>();
        }
    }
}
